/*
 * Copyright 2015-2016 fududu.org Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.fududu.oauth2;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;

import java.io.Serializable;

/**
 * The <a href="http://tools.ietf.org/html/rfc6749#section-4.1.2">RFC 6749#section-4.1.2</a> Error Response
 * standard oauth2.0 error model.
 *
 * @author Fuchun
 * @since 1.0
 */
public class StandardOAuth2Error implements OAuth2Error, Serializable {

    private static final long serialVersionUID = 1912379667793019464L;

    @JsonCreator
    public static StandardOAuth2Error of(
            @JsonProperty(PARAM_ERROR) String error,
            @JsonProperty(PARAM_ERROR_DESCRIPTION) String errorDescription,
            @JsonProperty(PARAM_ERROR_URI) String errorUri,
            @JsonProperty(PARAM_STATE) String state) {
        return new StandardOAuth2Error(error, errorDescription, errorUri, state);
    }

    private final String error;
    private final String errorDescription;
    private final String errorUri;
    private final String state;

    protected StandardOAuth2Error(String error, String errorDescription, String errorUri, String state) {
        this.error = error;
        this.errorDescription = errorDescription;
        this.errorUri = errorUri;
        this.state = state;
    }

    @Override
    public String getError() {
        return error;
    }

    @Override
    public String getErrorDescription() {
        return errorDescription;
    }

    @Override
    public String getErrorUri() {
        return errorUri;
    }

    @Override
    public String getState() {
        return state;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof StandardOAuth2Error)) return false;

        StandardOAuth2Error that = (StandardOAuth2Error) o;

        return that.toString().equals(toString());
    }

    @Override
    public int hashCode() {
        int result = error != null ? error.hashCode() : 0;
        result = 31 * result + (errorDescription != null ? errorDescription.hashCode() : 0);
        result = 31 * result + (errorUri != null ? errorUri.hashCode() : 0);
        result = 31 * result + (state != null ? state.hashCode() : 0);
        return result;
    }

    protected MoreObjects.ToStringHelper toStringHelper() {
        return MoreObjects.toStringHelper(getClass())
                .add(PARAM_ERROR, getError())
                .add("errorDescription", getErrorDescription())
                .add("errorUri", getErrorUri())
                .add(PARAM_STATE, getState());
    }

    @Override
    public String toString() {
        return toStringHelper().toString();
    }
}
